from mon_db_handler import MonDBHandler
from time import time

rw_handler = MonDBHandler.getInstance("","","/tmp")

def get_node_stats(nodeId, metricName, statisics="AVERAGE", period=1, \
                   startTime=None, endTime=None):
    """In the current implemention, nodeId equals to host's name or ip
    address; statistics should be one of AVERAGE, MAX and MIN
    """
    if endTime is None:
        endTime = int(round(time()))

    if startTime is None:
        startTime = endTime

    return rw_handler.read(nodeId, metricName, statisics, period, \
                           startTime, endTime)

if __name__ == "__main__":
    from time import sleep
    from mon_center_util import get_ip_address
    import random
    local = get_ip_address('eth0')
    hosts = [local]
    metrics = ['load_one', 'load_five', 'cpu_use_time']
    while True:
        i = 0
        j = random.randint(0, 2)
        print get_node_stats(hosts[i], metrics[j], "AVERAGE", 5, \
                             -60)
        sleep(6)
